// pages/classify/classify.js
import {
  Request
} from '../../http/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: 0,
    // 商品分类的数据
    classifyList: [],
    // 一级分类数据列表
    oneList: [],
    // 二级分类数据列表
    twoList: [],
    // 轮播图数据
    carlist: [{
        id: 1001,
        img: '/img/img/1.jpg'
      },
      {
        id: 1002,
        img: '/img/img/2.jpg'
      },
      {
        id: 1003,
        img: '/img/img/3.jpg'
      },
      {
        id: 1004,
        img: '/img/img/4.jpg'
      },
    ]
  },
  getClassify() {
    Request({url:'shop/goods/category/all'}).then(res => {
      this.data.oneList = res.data.filter(item => {
        return item.level === 1
      })
      this.data.twoList = this.data.classifyList = res.data
    
      this.setData({
        oneList: this.data.oneList,
        twoList: res.data
      })
    })
  },
  // 点击二级分类切换图片
  leftClick(e) {
    console.log(e);
    let index = e.currentTarget.dataset.index
    if (index === '0') {
      // 点击的是所有分类
      this.data.twoList = this.data.classifyList
      this.setData({ 
        twoList: this.data.twoList
      })
      console.log( this.data.twoList );
    }else{ 
          // 获取到左侧一级分类对应的type值
      let type=this.data.oneList[index].type
      // 根据左侧一级分类的type值从所有分类中筛选出对应的二级分类
      this.data.twoList=this.data.classifyList.filter(item=>{
       return item.type.includes(type)
      })
      console.log( this.data.twoList);
      this.setData({ 
        twoList:  this.data.twoList
      })
    }
  },
  // 跳转分类商品
  goodImgClick(e){ 
console.log(e);
let id=e.currentTarget.dataset.item.id
wx.navigateTo({
  url:'/pages/goodsclassify/goodsclassify?id='+id,
})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getClassify()
  },












})